import sys
import os
from pathlib import Path
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QSettings,QAbstractTableModel,Qt
from PySide6.QtGui import QIcon
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtQuickControls2 import QQuickStyle
import Bridge
import platform
import logging
from logging.handlers import RotatingFileHandler
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
sys.stderr = open(sys.stderr.fileno(), mode='w', encoding='utf8', buffering=1)
handlers=[RotatingFileHandler(filename="report.log", maxBytes=10*1024*1024, encoding='utf-8',backupCount=3),logging.StreamHandler(sys.stdout)]
logging.basicConfig(level=logging.DEBUG,
                    format = '%(asctime)s - %(name)s - %(levelname)s -%(funcName)s - %(message)s',
                    handlers=handlers)
logging.info('begin')
class TableModel(QAbstractTableModel):
    def rowCount(self,index):
        logging.info("here")
        return 10
    def columnCount(self,index):
        logging.info("here")
        return 10

    def data(self,index, role):
        logging.info("here")
        if (role == Qt.DisplayRole):
            return "%d, %d" % (index.column(),index.row())
        else:
            return None
def main():
    # if platform.uname().system=="Windows":
    #     pass
    # else:
    #     from Watcher import Watcher
    #     Watcher()
    QApplication.setApplicationName("report")
    # QApplication.setOrganizationName("QtProject")

    app = QApplication(sys.argv)
    # print(dir(app))
    # QIcon.setThemeName("gallery")

    settings = QSettings()
    if not os.environ.get("QT_QUICK_CONTROLS_STYLE"):
        style_name = settings.value("style")
        if style_name:
            QQuickStyle.setStyle(style_name)
    QQuickStyle.setStyle("Basic")
    engine = QQmlApplicationEngine()
    # built_in_styles = ["Basic", "Fusion", "Imagine", "Material", "Universal"]
    # if platform.system() == "Darwin":
    #     built_in_styles.append("macOS")
    # elif platform.system() == "Windows":
    #     built_in_styles.append("Windows")
    # engine.setInitialProperties({"builtInStyles": built_in_styles})

    # Get the path of the current directory, and then add the name
    # of the QML file, to load it.
    tablemodel=TableModel()
    engine.rootContext().setContextProperty("tableModel", tablemodel)
    qml_file = Path(__file__).parent / 'main.qml'
    # qml_file = Path(__file__).parent / 'C1.qml'
    engine.load(qml_file)

    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec())
if __name__ == '__main__':
    main()
    
